
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.DownloadsCategories;


public class GenericDownloadCategoriesDAO extends AutoKeys implements org.jetnuke.dao.DownloadCategoriesDAO {

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DowloadCategoriesModel#retrieveByPK(int)
	 */
	public DownloadsCategories retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadCatModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		DownloadsCategories s = new DownloadsCategories();
		
		if (rs.next()) {
			fillDownCatFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DowloadCategoriesModel#selectAll()
	 */
	public List selectAll() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadCatModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	private void fillDownCatFromResultSet(DownloadsCategories s, ResultSet rs)throws Exception {
		s.setCID(rs.getInt("CID"));
		s.setCDESCRIPTION(rs.getString("CDESCRIPTION"));
		s.setPARENTID(rs.getInt("PARENTID"));
		s.setTITLE(rs.getString("TITLE"));
		
	}
	protected List processSelectAll(ResultSet rs)throws Exception {
		List list = new ArrayList();
		while (rs.next()) {
			DownloadsCategories u = new DownloadsCategories();
			fillDownCatFromResultSet(u,rs);			
			list.add(u);
		}
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DowloadCategoriesModel#addNew(org.jetnuke.entities.DownloadsCategories)
	 */
	public int addNew(DownloadsCategories dc) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("DownloadCatModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("DownloadCategoriesModel.lastGeneratedDownloadCategoriesID"));
		initNewDownCat(dc, p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;
		
	}
	public int countAll() throws Exception {
		PreparedStatement p;
		
		
		p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadCatModel.count_all"));
		
		ResultSet rs = p.executeQuery();
		rs.next();
		
		
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
		
		
		
	}

	/**
	 * @param dc
	 * @param p
	 */
	private void initNewDownCat(DownloadsCategories dc, PreparedStatement p) throws Exception{
		p.setString(1,dc.getCDESCRIPTION());
		p.setString(2,dc.getTITLE());
		p.setInt(3,dc.getPARENTID());
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DowloadCategoriesModel#update(org.jetnuke.entities.DownloadsCategories)
	 */
	public void update(DownloadsCategories dc) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("DownloadCatModel.update");
		p.setString(1,dc.getCDESCRIPTION());
		p.setString(2,dc.getTITLE());
		p.setInt(3,dc.getPARENTID());
		p.setInt(4,dc.getCID());
		p.executeUpdate();
		p.close();
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadCategoriesModel#selectAllOrderTitleAsc()
	 */
	public List selectAllOrderTitleAsc() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadCatModel.selectAllOrderTitleAsc"));
			
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadCategoriesModel#selectByParent(int)
	 */
	public List selectByParent(int id) throws Exception {
		PreparedStatement p;
		
		
		p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadCatModel.selectByParent"));
		p.setInt(1,id);	
				
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadCategoriesModel#selectAllOrderParentAsc()
	 */
	public List selectAllOrderParentAsc() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadCatModel.selectAllOrderParentAsc"));
		
	ResultSet rs = p.executeQuery();
	List list = this.processSelectAll(rs);
	rs.close();
	p.close();
	
	return list;
	}
}
